package com.example.controller;

import com.example.entity.ArticleEntity;
import com.example.entity.query.ArticlePagingQueryFilter;
import com.example.entity.viewModel.BatchUpdateArticleStatusRequest;
import com.example.entity.viewModel.UpdateArticleStatusRequest;
import com.example.enums.ArticleStatusEnum;
import com.example.service.ArticleService;
import com.example.common.base.PagingQueryResult;

import io.swagger.annotations.Api;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by James.H.Fu on 2016/12/17.
 */
@RestController
@RequestMapping("/article")
@Api("文章相关接口")
public class ArticleController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ArticleService articleService;

    @PostMapping("/insert")
    public void insert(@RequestBody ArticleEntity article) {
        articleService.insert(article);
        String info = "自增列ArticleID=" + article.getArticleID();
        logger.debug(info);
    }

    @PostMapping("/batchInsert")
    public void batchInsert(@RequestBody List<ArticleEntity> articleList) {
        articleService.batchInsert(articleList);

        StringBuilder sb = new StringBuilder();
        for (ArticleEntity item : articleList) {
            sb.append("自增列ArticleID=" + item.getArticleID() + "\r\n");
        }
        logger.debug(sb.toString());
    }

    @PutMapping("/update")
    public void update(@RequestBody ArticleEntity article) {
        articleService.update(article);
    }

    @PutMapping("/batchUpdate")
    public void batchUpdate(@RequestBody List<ArticleEntity> articleList) {
        articleService.batchUpdate(articleList);
    }

    @GetMapping("/queryAll")
    public List<ArticleEntity> queryAll() {
        return articleService.queryAll();
    }

    @PostMapping("/pagingQuery")
    public PagingQueryResult<ArticleEntity> pagingQuery(@RequestBody ArticlePagingQueryFilter filter) {
        return articleService.pagingQuery(filter);
    }

    @PutMapping("/updateStatus")
    public int updateStatus(@RequestBody UpdateArticleStatusRequest req) {
        return articleService.updateStatus(req);
    }

    @PutMapping("/batchUpdateStatus")
    public int batchUpdateStatus(@RequestBody BatchUpdateArticleStatusRequest req) {
        return articleService.batchUpdateStatus(req);
    }

    @DeleteMapping("/delete/{id}")
    public int delete(@PathVariable("id") int id) {
        return articleService.delete(id);
    }

    @DeleteMapping("/batchDeleteByArray")
    public int batchDelete(@RequestBody Object[] idArray) {
        return articleService.batchDelete(idArray);
    }

    @DeleteMapping("/batchDeleteByList")
    public int batchDelete(@RequestBody List<Integer> idList) {
        return articleService.batchDelete(idList);
    }

    @GetMapping("/load/{id}")
    public ArticleEntity load(@PathVariable("id") int id) {
        ArticleEntity result = articleService.load(id);
        if (result == null) {
            result = new ArticleEntity();
            result.setArticleID(0);
        }
        return result;
    }

    @GetMapping("/loadMaxArticleID")
    public Integer loadMaxArticleID() {
        return articleService.loadMaxArticleID();
    }

    @GetMapping("/loadMaxPublishDate")
    public Date loadMaxPublishDate() {
        return articleService.loadMaxPublishDate();
    }

    @GetMapping("/loadFirstTitle")
    public String loadFirstTitle() {
        return articleService.loadFirstTitle();
    }

    @GetMapping("/loadArticleIDList")
    public List<Integer> loadArticleIDList() {
        return articleService.loadArticleIDList();
    }

    @GetMapping("/loadArticleStatusEnum")
    public List<Map<String, Object>> loadArticleStatusEnum() {
        return ArticleStatusEnum.getDescriptionList();
    }
}
